package LeetCode.leetcode429;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
}

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        Queue<Node> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        Node tmp;
        int length;
        if (root == null)
            return  res;
        queue.offer(root);
        while (!queue.isEmpty()){
            List<Integer> lines = new ArrayList<>();
            length = queue.size();
            while (length > 0){
                //队头出队
                tmp = queue.poll();
                //添加当前队友的值
                lines.add(tmp.val);
                //入队
                for (int i = 0; i < tmp.children.size(); i++) {
                    queue.offer(tmp.children.get(i));
                }
                length--;
            }
            res.add(lines);
        }
        return res;
    }
}
